package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.system.domain.SysMapSign;
import com.ruoyi.system.service.ISysMapSignService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 园区地图标点Controller
 *
 * @author yangyu
 * @date 2022-11-10
 */
@Api(tags = " 园区地图标点")
@RestController
@RequestMapping("/sysMapSign")
public class SysMapSignController extends BaseController {
    @Autowired
    private ISysMapSignService sysMapSignService;

    /**
     * 查询园区地图标点列表
     */
    @ApiOperation(value="查询园区地图标点列表", notes="查询园区地图标点列表")
    @PreAuthorize(hasPermi = "system:sysMapSign:list")
    @GetMapping("/list")
    public TableDataInfo list(SysMapSign sysMapSign) {
        startPage();
        List<SysMapSign> list = sysMapSignService.selectSysMapSignList(sysMapSign);
        return getDataTable(list);
    }

    /**
     * 导出园区地图标点列表
     */
    @ApiOperation(value="导出园区地图标点列表", notes="导出园区地图标点列表")
    @PreAuthorize(hasPermi = "system:sysMapSign:export")
    @Log(title = "园区地图标点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMapSign sysMapSign) throws IOException {
        List<SysMapSign> list = sysMapSignService.selectSysMapSignList(sysMapSign);
        ExcelUtil<SysMapSign> util = new ExcelUtil<SysMapSign>(SysMapSign.class);
        util.exportExcel(response, list, "园区地图标点数据");
    }
    /**
     * 获取园区地图标点详细信息
     */
    @ApiOperation(value="获取园区地图标点详细信息", notes="获取园区地图标点详细信息")
    @PreAuthorize(hasPermi = "system:sysMapSign:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysMapSignService.selectSysMapSignById(id));
    }

    /**
     * 新增园区地图标点
     */
    @ApiOperation(value="新增园区地图标点", notes="新增园区地图标点")
    @PreAuthorize(hasPermi = "system:sysMapSign:add")
    @Log(title = "园区地图标点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMapSign sysMapSign) {
        return toAjax(sysMapSignService.insertSysMapSign(sysMapSign));
    }

    /**
     * 批量新增园区地图标点
     */
    @ApiOperation(value="批量新增园区地图标点", notes="批量新增园区地图标点")
    @PreAuthorize(hasPermi = "system:sysMapSign:add")
    @Log(title = "批量新增园区地图标点", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<SysMapSign> sysMapSigns) {
        int errCountNum=0;
        List<String> errItem=new ArrayList<>();
        for (SysMapSign sysMapSign : sysMapSigns){
            sysMapSign.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
            sysMapSign.setCreateTime(DateUtils.getNowDate());
            sysMapSign.setCreateBy(SecurityUtils.getUsername());
            int resNum=  sysMapSignService.insertSysMapSign(sysMapSign);
            if (resNum==0){
                errCountNum+=1;
                errItem.add(sysMapSign.getPointCode());
            }
        }
        if (errCountNum>0){
            return AjaxResult.error(errItem.toString()+"插入失败,失败条数："+errCountNum);
        }else {
            return AjaxResult.success();
        }

    }


    /**
     * 修改园区地图标点
     */
    @ApiOperation(value="修改园区地图标点", notes="修改园区地图标点")
    @PreAuthorize(hasPermi = "system:sysMapSign:edit")
    @Log(title = "园区地图标点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMapSign sysMapSign) {
        return toAjax(sysMapSignService.updateSysMapSign(sysMapSign));
    }

    /**
     * 删除园区地图标点
     */
    @ApiOperation(value="删除园区地图标点", notes="删除园区地图标点")
    @ApiImplicitParam(name = "ids", value = "园区地图标点ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "system:sysMapSign:remove")
    @Log(title = "园区地图标点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysMapSignService.deleteSysMapSignByIds(ids));
    }
}
